package seGobangClient.onlineHall;

import java.util.*;
import GameHallClient.*;

public class Desk {
    private static final int DESK_STATE_EMPTY=0,              //空
                        DESK_STATE_WAITING=1,           //一人加入
                        DESK_STATE_STANDBY=2,           //两人加入
                        DESK_STATE_READY=3,             //一方准备
                        DESK_STATE_GAMING=4;            //游戏开始
    private OnlineHallController MyController;
    public int State;
    public PlayerInfo playerLocal, playerRemote;
    Room MyRoom;
    public int DeskID;
    public boolean IAmReady;
    private boolean IAmHere;
    private Timer UpdateTimer=null;
    private GameHallClient hall=new GameHallClient();

    public Desk(OnlineHallController MyController,Room MyRoom, int DeskID) {
        this.MyController=MyController;
        this.MyRoom = MyRoom;
        this.DeskID = DeskID;
        IAmReady=false;
        this.IAmHere=false;
    }
    
    private void startStateUpdate(){
        if (UpdateTimer !=null) UpdateTimer.cancel();
        UpdateTimer=new Timer();
        UpdateTimer.schedule(new TimerTask(){
            /**
             * 本地玩家加入棋桌，开始刷新远程家信息
             */
            public void run() {
                if(State==DESK_STATE_STANDBY || State==DESK_STATE_READY){
                    playerRemote=hall.GetRemotePlayerInfo(MyRoom.RoomID, DeskID, (int)OnlineHallController.UID);    
                    
                    if(playerRemote!=null){
                        MyController.doEvent("RefreshRemotePlayerInfo", playerRemote);                    
                    }
                    
                }
            }
            
        }, 0, OnlineHallController.updateRate);
    }
    
    public void JoinDesk(){
        playerLocal=hall.GetPlayerInfo((int)OnlineHallController.UID);
        hall.takeGame((int)OnlineHallController.UID, MyRoom.RoomID, DeskID);     
        
        MyController.doEvent("DeskView", DeskID);
        this.IAmHere=true;
        startStateUpdate();
    }
    
    public void ExitDesk(){
        hall.quitGame((int)OnlineHallController.UID, MyRoom.RoomID, DeskID);
       
        this.IAmHere=false;
        if (UpdateTimer !=null) UpdateTimer.cancel();
        UpdateTimer=null;
        MyController.doEvent("RoomView", MyRoom.RoomName);
    }

    public void GetReady(){
        GameHallClient hallClient=new GameHallClient();
        hallClient.getReady(MyRoom.RoomID, DeskID);
        this.IAmReady=true;
    }
 
    public void CancelReady(){
        GameHallClient hallClient=new GameHallClient();
        hallClient.cancelReady(MyRoom.RoomID, DeskID);
        this.IAmReady=false;
    }
    
    public int getState() {
        return State;
    }

    public void pauseDesk(){
        if (UpdateTimer !=null) UpdateTimer.cancel();
    }
    
    public void resumeDesk(){
        this.IAmReady=false;
        startStateUpdate();
    }
    
    public void setState(int State) {
        int formerState=this.State;
        this.State=State;
        if (!IAmHere){
            this.State = State;
            return;
        }else{
            if (State!=formerState){
                switch(State){
                    /*case DESK_STATE_WAITING:
                        MyController.doEvent("DeskWaiting", null);
                        break;
                    case DESK_STATE_STANDBY:
                        MyController.doEvent("DeskStandby", null);
                        break;
                    case DESK_STATE_READY:
                        if (this.IAmReady)
                            MyController.doEvent("DeskLocalReady", null);
                        else
                            MyController.doEvent("DeskRemoteReady", null);
                        break;*/
                    case DESK_STATE_GAMING:
                        MyController.doEvent("StartGame", this);
                        break;
                        
                }
            }
        }
    }
    
    
}
